//
//  SharePDF.swift
//  CheckmeRing
//
//  Created by 董鹏 on 2022/3/12.
//

import UIKit

class SharePDF: UIView {

    /// 分享的数据(模型)
    var historyData = [RModel]()
    
    @IBOutlet weak var notesLabel: UILabel!
    @IBOutlet weak var view1: UIView!
    @IBOutlet weak var view2: UIView!
    @IBOutlet weak var view3: UIView!
    @IBOutlet weak var view4: UIView!
    @IBOutlet weak var view5: UIView!
    
    /// 时间信息
    @IBOutlet weak var timeLabel1: UILabel!
    @IBOutlet weak var timeLabel2: UILabel!
    @IBOutlet weak var timeLabel3: UILabel!
    @IBOutlet weak var timeLabel4: UILabel!
    @IBOutlet weak var timeLabel5: UILabel!
    
    // 平均值信息 从左到右 从上到下
    @IBOutlet weak var valueLabel1: UILabel!
    @IBOutlet weak var valueLabel2: UILabel!
    @IBOutlet weak var valueLabel3: UILabel!
    @IBOutlet weak var valueLabel4: UILabel!
    @IBOutlet weak var valueLabel5: UILabel!
    @IBOutlet weak var valueLabel6: UILabel!
    
    @IBOutlet weak var RatioView: UIView!
    @IBOutlet weak var SpO2BgView: UIView!
    @IBOutlet weak var PRBgView: UIView!
    @IBOutlet weak var MotionBgView: UIView!
    
    var RatioChartView = BarChartView()
    var SpO2ChartView = LineChartView()
    var PRChartView = LineChartView()
    var MotionChartView = LineChartView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    
    func setUI() {
        
        self.layerView(view: view1)
        self.layerView(view: view2)
        self.layerView(view: view3)
        self.layerView(view: view4)
        self.layerView(view: view5)
        
        let width = Int(self.SpO2BgView.width - 0)
        let top = 25
        // 血氧
        self.SpO2ChartView = DetailChartViewTool.initDetailChartView(data: self.historyData, isSpO2: true)
        self.SpO2ChartView.frame = CGRect(x: 0, y: top, width: width, height: Int(self.SpO2BgView.height) - top)
        self.SpO2BgView.addSubview(self.SpO2ChartView)
        
        // 脉搏
        self.PRChartView = DetailChartViewTool.initDetailChartView(data: self.historyData, isSpO2: false, sharePDF: true)
        self.PRChartView.frame = CGRect(x: 0, y: top, width: width, height: Int(self.SpO2ChartView.height))
        self.PRBgView.addSubview(self.PRChartView)
        
        // 运动
        self.MotionChartView = DetailChartViewTool.initMotionChartView(data: self.historyData)
        self.MotionChartView.frame = CGRect(x: 0, y: top, width: width, height: Int(self.SpO2ChartView.height))
        self.MotionBgView.addSubview(self.MotionChartView)
        
        // 占比
        self.RatioChartView = DetailChartViewTool.initRatioBarChartView(data: self.historyData)
        self.RatioChartView.frame = CGRect(x: 0, y: top, width: Int(self.RatioView.width), height: Int(self.RatioView.height) - top)
        self.RatioView.addSubview(self.RatioChartView)
        
    }
    
    func setData(list: [RModel]) {
        self.historyData = list
        self.setUI()
        if historyData.count < 1 {
            return
        }
        
        // 笔记
        if let notes = historyData.first!.notes {
            self.notesLabel.text = String(format: "Note：%@", notes.joined(separator: "、"))
        }
        
        timeLabel1.text = "Start date：\(formatTimeStamp(time: self.historyData.first!.timeStamp, format: "MMM dd,yyyy"))"
        timeLabel2.text = "Start Time：\(formatTimeStamp(time: self.historyData.first!.timeStamp, format: "HH:mm"))"
        timeLabel3.text = "End time：\(formatTimeStamp(time: self.historyData.last!.timeStamp, format: "HH:mm"))"
        
        /// 记录时长
        let time = abs(Int(self.historyData.first!.timeStamp) - Int(self.historyData.last!.timeStamp)) + ReadRate
        timeLabel4.text = "Duration：" + FormatDate(second: time)
        
        // 低于90%时长、平均血氧、最低血氧、平均脉率、最高血氧、最高脉率、最低脉率
        var less90 = 0, totalSpO2 = 0, minSpO2 = 100, totalHR = 0, highSpO2 = 0, highPR = 0, minPR = 200
        for obj in self.historyData {
            if obj.spo2 < 90 {
                less90 += ReadRate
            }
            totalSpO2 += obj.spo2
            minSpO2 = min(minSpO2, obj.spo2)
            totalHR += obj.hr
            highSpO2 = max(highSpO2, obj.spo2)
            highPR = max(highPR, obj.hr)
            minPR = min(minPR, obj.hr)
        }
        timeLabel5.text = "<90% 時間：" + FormatDate(second: less90)
        
        valueLabel1.text = String(highSpO2)
        valueLabel2.text = String(totalSpO2 / self.historyData.count)
        valueLabel3.text = String(minSpO2)
        valueLabel4.text = String(highPR)
        valueLabel5.text = String(totalHR / self.historyData.count)
        valueLabel6.text = String(minPR)
        
    }
    
    func layerView(view: UIView) {
        view.layer.borderWidth = 1
        view.layer.borderColor = UIColor.black.cgColor
    }

}
